package sa.com.sure.ejtimaa.database;


import java.text.ParseException;

import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.model.MemberExecuseInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class MemberExecuses {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "MemberExecuses";
	
    public static final String ExecuseIDField = "ExecuseID"; 
    public static final String MeetingAttendeeIDField = "MeetingAttendeeID";
    public static final String ExecuseTextField = "ExecuseText";
    public static final String CreatedDateTimeField = "CreatedDateTime";    
    
    private String[] allColumns = { ExecuseIDField, MeetingAttendeeIDField,ExecuseTextField,CreatedDateTimeField};
    
    Cursor cursor = null;
    
    public MemberExecuses(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static MemberExecuses Instance = new MemberExecuses(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
		
	
	public MemberExecuseInfo GetItem(int ExecuseID) throws SQLException, ParseException
	{	
		try	
		{
			open();
		
			MemberExecuseInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, ExecuseIDField+ "=" + ExecuseID ,null,null,null,null);
        	if (cursor.moveToFirst()) 	        	
        		item =  getItemInfo(cursor);	     
    	        
        	return 	item;
        	
		}
        finally{        	
            close();
        }    		        	   
	}
		
	
	public void addItem(MemberExecuseInfo item) throws ParseException 
	{ 
		try	
		{        
			open();
			
	        ContentValues values = new ContentValues(); 
	        
	        values.put(ExecuseIDField, item.GetExecuseID());  
	        values.put(MeetingAttendeeIDField, item.GetMeetingAttendeeID());  
	        values.put(ExecuseTextField, item.GetExecuseText());
	        values.put(CreatedDateTimeField, Helper.GetDateAsStringFormat(item.GetCreatedDateTime(),Helper.DateFormatWebService));
	        
	        dbHelper.db.insert(TABLE_Name, null, values); 
        
		}
	    finally{        	
	        close();
	    }  
                
    } 
	
	 public boolean updateItem(MemberExecuseInfo item) throws ParseException 
	 {
		 try	
			{		 
			 	open();
			 	
			    ContentValues values = new ContentValues();		    
			    
		        values.put(MeetingAttendeeIDField, item.GetMeetingAttendeeID());  
		        values.put(ExecuseTextField, item.GetExecuseText());
		        values.put(CreatedDateTimeField, Helper.GetDateAsStringFormat(item.GetCreatedDateTime(),Helper.DateFormatWebService));
 
			    
			    return dbHelper.db.update(TABLE_Name, values, ExecuseIDField + "=" + item.GetExecuseID(), null) > 0;			    			   
			    			    	        
			}
		    finally{        	
		        close();
		    }  	               
	 }
	
	
    private MemberExecuseInfo getItemInfo(Cursor cursor) throws ParseException {  
    	
    	MemberExecuseInfo itemInfo = new MemberExecuseInfo();
    	
    	itemInfo.SetExecuseID(cursor.getInt(cursor.getColumnIndex(ExecuseIDField)));    	
    	itemInfo.SetMeetingAttendeeID(cursor.getInt(cursor.getColumnIndex(MeetingAttendeeIDField)));    	
    	itemInfo.SetExecuseText(cursor.getString(cursor.getColumnIndex(ExecuseTextField)));
    	itemInfo.SetCreatedDateTime(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(CreatedDateTimeField)),Helper.DateFormatWebService));
    	
		return itemInfo;
	}
}
